﻿using Pandora;
using UnityEditor;
using UnityEditor.U2D;
using UnityEngine;
using UnityEngine.U2D;

namespace PandoraEditor
{
    public static class EffectAtlasMaker
    {
        private static readonly string actionName = "ExecuteWrapEffectAtlas";
        public static readonly string input_path = "Assets/Art/effect/atlas/";
        private static readonly string output_path = "Assets/Art/effect_atlas_config/";

        public static void ExecuteWrapEffectAtlas()
        {
            PdrFileUtil.DeleteDirectory(output_path);
            PdrFileUtil.CreateDirectory(output_path);
            var list = PdrFileUtil.GetDirsName(input_path);
            foreach (var name in list)
            {
                PackSingleEffectAtlas(name);
            }

            Debug.Log($"{actionName}运行完毕");
            AssetDatabase.Refresh();
        }

        public static void PackSingleEffectAtlas(string name)
        {
            PdrFileUtil.CreateDirectory(output_path);
            SpriteAtlas spriteAtlas = new SpriteAtlas();
            SpriteAtlasPackingSettings packingSettings = new SpriteAtlasPackingSettings
            {
                blockOffset = 1,
                padding = 4,
                enableRotation = false,
                enableTightPacking = false
            };
            spriteAtlas.SetPackingSettings(packingSettings);
            SpriteAtlasTextureSettings textureSettings = new SpriteAtlasTextureSettings
            {
                readable = false,
                generateMipMaps = false,
                sRGB = true,
                filterMode = FilterMode.Bilinear
            };
            spriteAtlas.SetTextureSettings(textureSettings);
            Object folder = AssetDatabase.LoadAssetAtPath<Object>($"{input_path}{name}");
            spriteAtlas.Add(new[] { folder });
            AssetDatabase.CreateAsset(spriteAtlas, $"{output_path}{name}.spriteatlas");
            AssetDatabase.SaveAssets();
        }
    }
}